package com.polaris.live.ui.vip.fragment

import android.os.Bundle
import androidx.core.os.bundleOf
import com.blankj.utilcode.util.StringUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.databinding.FragmentVipCenterMainBinding
import com.polaris.live.resp.back_resp.UserVipCenterResp
import com.polaris.live.translation.R
import com.polaris.live.widget.tab.TabBean

/**
 * VipCenterMainFragment
 *
 * @author Created by 天晴 on 2024/12/10 10:40
 * @since 1.0.0
 **/
class VipCenterMainFragment : BaseVbFragment<BaseViewModel, FragmentVipCenterMainBinding>() {

    private val mLevel by lazy { arguments?.getInt("level") ?: -1 }

    private val mFragments = ArrayList<VipCenterLevelFragment>()
    private val mTitles = ArrayList<TabBean>()
    private var mTabAdapter: ViewPagerAdapter? = null
    private var mCurrentItemIndex = 0

    private var mUserVipCenterResp: UserVipCenterResp? = null

    private var mNowShowLevel: Int = -1

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .titleBarMarginTop(mBinding.titleRl)
            .navigationBarDarkIcon(false)
            .transparentNavigationBar()
            .init()

        mNowShowLevel = mLevel
        mTabAdapter = ViewPagerAdapter(mFragments, this)
        mBinding.viewpager2.adapter = mTabAdapter
        mBinding.tab.setViewPage(mBinding.viewpager2)

        repeat(6) {
            mFragments.add(VipCenterLevelFragment().apply {
                if (mNowShowLevel != -1 && mNowShowLevel == it + 1) {
                    mCurrentItemIndex = it
                }
                arguments = bundleOf(
                    "level" to (it + 1).toLong()
                )
                upData {
                    upData?.invoke()
                }
                toVip { level ->
                    toMineVip?.invoke(level)
                }
            })
            mTitles.add(TabBean(StringUtils.getString(R.string.vip_level, it + 1)))
        }
        mTabAdapter?.setFragments(mFragments)
        mBinding.tab.setTabs(mTitles)
        mBinding.viewpager2.setCurrentItem(mCurrentItemIndex, false)
        mBinding.tab.onPageSelected(mCurrentItemIndex)
        mBinding.viewpager2.offscreenPageLimit = mFragments.size

    }

    fun goPageSelected(level: Int) {
        withLoaded {
            val po = mFragments.indexOfFirst { it.mLevel.toInt() == level }
            if (po != -1) {
                mBinding.tab.onPageSelected(po)
                mBinding.viewpager2.setCurrentItem(po, true)
            } else {
                mNowShowLevel = level
            }
        }
    }

    fun setData(mUserVipCenterResp: UserVipCenterResp) {
        this.mUserVipCenterResp = mUserVipCenterResp
        withLoaded {
            mUserVipCenterResp.levels.forEach {
                mFragments.getOrNull((it.level - 1).toInt().coerceIn(0, 6))?.apply {
                    setData(it)
                }
            }
        }
    }

    private var upData: (() -> Unit)? = null
    fun upData(back: () -> Unit) {
        this.upData = back
    }

    private var toMineVip: ((index: Int) -> Unit)? = null
    fun toVip(toMineVip: (index: Int) -> Unit) {
        this.toMineVip = toMineVip
    }
}